using System;
using System.Collections.Generic;
using System.Text;
using SMS.Common.Entities;
using System.Data.Common;

namespace SMS.Common.Data
{
    public sealed class LogDB
    {
        public static void AddLog(string db, Log log)
        {
            using (DbConnection con = SQLite.GetDbConnection())
            {
                con.ConnectionString = db;
                con.Open();
                using (DbCommand comm = con.CreateCommand())
                {
                    comm.CommandText = string.Format("insert into tbl_log(Application,Module,Function,Type,Date,Time,Content) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", log.App, log.ModuleName,log.Function, log.Type, log.Date, log.Time, log.Content);
                    comm.ExecuteNonQuery();
                }
            }
        }

        public static void DeleteLog(string db, DateTime? beginDate, DateTime? endDate,string app,string module)
        {
            string strCondition = string.Empty;
            if (beginDate.HasValue && endDate.HasValue)
            {
                strCondition = string.Format(" and date between '{0}' and '{1}'",beginDate.Value.ToString("yyyy-MM-dd"),endDate.Value.ToString("yyyy-MM-dd"));
            }
            using (DbConnection con = SQLite.GetDbConnection())
            {
                con.ConnectionString = db;
                con.Open();
                using (DbCommand comm = con.CreateCommand())
                {
                    comm.CommandText = string.Format("delete from tbl_log where Application='{0}' and Module='{1}'",app,module) +strCondition;
                    comm.ExecuteNonQuery();
                }
            }
        }

        public static List<Log>  GetLogs(string db, DateTime Date,string app,string module,string function)
        {
            List<Log> list = new List<Log>();
            using (DbConnection con = SQLite.GetDbConnection())
            {
                con.ConnectionString = db;
                con.Open();
                using (DbCommand comm = con.CreateCommand())
                {
                    comm.CommandText = string.Format("select * from tbl_log where date='{0}' and Application='{1}' and Module='{2}' and Function='{3}'",Date.ToString("yyyy-MM-dd"),app,module,function);
                    DbDataReader reader = comm.ExecuteReader();
                    while (reader.Read())
                    {
                        Log l = new Log();
                        l.App = reader["Application"].ToString();
                        l.ModuleName = reader["Module"].ToString();
                        l.Type = reader["Type"].ToString();
                        l.Date = DateTime.Parse(reader["Date"].ToString()).ToString("yyyy-MM-dd");
                        l.Time = DateTime.Parse(reader["Time"].ToString()).ToString("hh:mm:ss");
                        l.Content = reader["Content"].ToString();
                        l.Function = reader["Function"].ToString();
                        list.Add(l);
                    }
                    reader.Close();
                }
            }
            return list;
        }
        public static List<Log> GetLogs(string db, DateTime Date, string app, string module)
        {
            List<Log> list = new List<Log>();
            using (DbConnection con = SQLite.GetDbConnection())
            {
                con.ConnectionString = db;
                con.Open();
                using (DbCommand comm = con.CreateCommand())
                {
                    comm.CommandText = string.Format("select * from tbl_log where date='{0}' and Application='{1}' and Module='{2}' order by time", Date.ToString("yyyy-MM-dd"), app, module);
                    DbDataReader reader = comm.ExecuteReader();
                    while (reader.Read())
                    {
                        Log l = new Log();
                        l.App = reader["Application"].ToString();
                        l.ModuleName = reader["Module"].ToString();
                        l.Type = reader["Type"].ToString();
                        l.Date = DateTime.Parse(reader["Date"].ToString()).ToString("yyyy-MM-dd");
                        l.Time = DateTime.Parse(reader["Time"].ToString()).ToString("hh:mm:ss");
                        l.Content = reader["Content"].ToString();
                        l.Function = reader["Function"].ToString();
                        list.Add(l);
                    }
                    reader.Close();
                }
            }
            return list;
        }
    }
}
